<?php
/**
 * 系统变量类
 */

class SysVar extends MyBase{
	
	function __construct() {
		parent::__construct('sysvar.id', array('keyword' => 'key'), $cacher = 'cache_sysvar.sysvars');
	}


	public function update($id, $post){
		if(isset($post['value'])){
			$post['value'] = $this->format_value_in($post['type'], $post['value']);
		}
		$post['updatetime'] = empty($post['updatetime']) ? TIME : strtotime($post['updatetime']);
		return $this->db->update($this->tbname, array('id' => $id), $post);
	}
	
	/**
	 * 修改系统变量
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		$select = array('type' => $post['type'], 'key' => $post['key']);
		if($id > 0){
			$select['kept'] = " AND id!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)) {
			return $this->rwarn(lang('already') . lang('exist'));
		}
		$data = array(
			'type'		=> $post['type'],
			'key'		=> $post['key'],
			'value'		=> $this->format_value_in($post['type'], $post['value']),
			'state'		=> isset($post['state']) ? intval($post['state']) : 0,
			'memo'		=> $post['memo'],
			'updatetime'=> TIME,
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$r = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$r = $id = $this->db->insert($this->tbname, $data);
		}
		if($r > 0){
			return $this->cache_sysvar();
		}
		return $r;
	}
			
	/**
	 * 删除系统变量
	 */
	public function del($post){
		if(!is_array($post['list']))$post['list'] = array($post['list']);
		$iRtn = 0;
		foreach($post['list'] as $id){
			if($this->db->update($this->tbname, array('id' => $id, 'system' => 0), array('isdel' => 1))){
				$iRtn ++;
			}
		}
		if($iRtn > 0){
			$this->cache_sysvar();
		}
		return $iRtn;
	}
	
	/**
	 * 取一条记录
	 */
	/*public function get_row($id, $fields = '*'){
		return $this->db->get_row($this->tbname, array('id' => $id), $fields);
	}*/
	
	/**
	 * 取列表
	 */
	/*public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		$select['kept'] = '';
		if(!empty($select['keyword'])){
			$select['kept'] .= " AND `key` LIKE '%{$select['keyword']}%'";
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}*/
		
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
	
	public function cache_sysvar(){
		/*******************************************************************************
		 * 商城配置
		 *******************************************************************************/
		/*$sysvar_list = $this->db->get_all($this->tbname, '`type`,`key`,`value`', array('system' => 1, 'isdel' => 0));
		$shop_config = shop_config_get();
		foreach($sysvar_list['list'] as $item){
			if($item['type'] == '1'){
				$item['value'] = unserialize($item['value']);
			}elseif($item['type'] == '2'){
				$item['value'] = intval($item['value']);
			}else{
				$item['value'] = trim($item['value']);
			}
			$shop_config[$item['key']] = $item['value'];
		}
		shop_config_save($shop_config);*/

		/*******************************************************************************
		 * 后台配置
		 *******************************************************************************/
		$sysvar_list = $this->db->get_all($this->tbname, '`type`,`key`,`value`', array('isdel' => 0));
		$sysvars = array();
		foreach($sysvar_list['list'] as $item){
			if($item['type'] == '1'){
				$item['value'] = unserialize($item['value']);
			}elseif($item['type'] == '2'){
				$item['value'] = intval($item['value']);
			}else{
				$item['value'] = trim($item['value']);
			}
			$sysvars[$item['key']] = $item['value'];
		}
		return cache_write('sysvars.php', $sysvars);
	}
	
	public function format_value_in($type, $value){
		if($type == 1){
			$arr = array();
			$str = preg_replace('/(\n|；)/', ';', $value);
			// $str = str_replace('：', ':', $str);
			$_arr = explode(';', $str);
			foreach($_arr as $item){
				$_tmp = explode(':', $item, 2);
				$_tmp[0] = trim($_tmp[0]);
				if(empty($_tmp[0]))continue;
				if(isset($_tmp[1])){
					$arr[$_tmp[0]] = trim($_tmp[1]);
				}else{
					$arr[] = $_tmp[0];
				}
			}
			return serialize($arr);
		}elseif($type == 2){		
			return intval($value);
		}elseif($type == 3){
			return trim($value);
		}
		return null;
	}
	
	public function format_value_out($type, $value){
		if($type == 1){
			$_arr = @unserialize($value);
			$arr = array();
			foreach($_arr as $key => $val){
				$arr[] = "$key:$val";
			}
			return implode("\n", $arr);
		}elseif($type == 2){
			return intval($value);
		}elseif($type == 3){
			return trim($value);
		}
		return null;
	}
}
?>